package com.ideafactory.client.views;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ideafactory.client.beans.CatalogGoodsBean;
import com.ideafactory.client.utils.CommonUtils;
import com.ideafactory.client.utils.HintViewUtils;
import com.ideafactory.client.utils.TextVUtils;
import com.ideafactory.client.ybservice.APP;
import com.ideafactory.client.ybselfservice.R;
import com.ideafactory.client.ybservice.ybviewpager.GoodsEnsurePager;

import java.util.List;

/**
 * Created by LiuShao on 2016/4/19.
 */
public class GoodsEnsureAdapter extends RecyclerView.Adapter<GoodsEnsureAdapter.GoodsEnsureHolder> {


    public void setCatalogGoodsLists(List<CatalogGoodsBean> catalogGoodsLists) {
        this.catalogGoodsLists = catalogGoodsLists;
    }

    public List<CatalogGoodsBean> getCatalogGoodsLists() {
        return catalogGoodsLists;
    }

    private List<CatalogGoodsBean> catalogGoodsLists;

    @Override
    public GoodsEnsureHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(APP.getAPP().getApplicationContext()).inflate(R.layout.good_ensure_item, parent, false);
        return new GoodsEnsureHolder(view);
    }

    @Override
    public void onBindViewHolder(final GoodsEnsureHolder holder, final int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return catalogGoodsLists.size();
    }

    class GoodsEnsureHolder extends RecyclerView.ViewHolder {
        TextView goods_ensure_name;
        Button btn_goods_sub;
        TextView tv_goods_des;
        Button btn_goods_add;
        TextView tv_goods_ensure_money_left;
        TextView goods_ensure_money;
        ImageView good_ensure_clear;

        public GoodsEnsureHolder(View itemView) {
            super(itemView);
            goods_ensure_name = (TextView) itemView.findViewById(R.id.goods_ensure_name);
            btn_goods_sub = (Button) itemView.findViewById(R.id.btn_goods_sub);
            tv_goods_des = (TextView) itemView.findViewById(R.id.tv_goods_describ);
            btn_goods_add = (Button) itemView.findViewById(R.id.btn_goods_add);
            tv_goods_ensure_money_left = (TextView) itemView.findViewById(R.id.tv_goods_ensure_money_left);
            goods_ensure_money = (TextView) itemView.findViewById(R.id.goods_ensure_money);
            good_ensure_clear = (ImageView) itemView.findViewById(R.id.good_ensure_clear);
        }

        public void setData(final int position) {
            goods_ensure_name.setText(catalogGoodsLists.get(position).getName());
            btn_goods_sub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    upDateItemSub(position);
                }
            });

            good_ensure_clear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (GoodsEnsurePager.onReceivedQuantityChange != null) {
                        GoodsEnsurePager.onReceivedQuantityChange.onReceivedDelete(position,catalogGoodsLists.get(position).getIsPackCharge());
                    }
                }
            });


            tv_goods_des.setText(catalogGoodsLists.get(position).getGoodsAdded() + "");
            tv_goods_ensure_money_left.setText(TextVUtils.highlight(catalogGoodsLists.get(position).getPrice() + ""));
            goods_ensure_money.setText(TextVUtils.highlight(APP.getAPP().getResources().getString(R.string.money_unit) + CommonUtils.mulNum(catalogGoodsLists.get(position).getPrice(),catalogGoodsLists.get(position).getGoodsAdded())));
            btn_goods_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    upDateItemAdd(position);
                }
            });
        }


        private void upDateItemAdd(int position) {
            CatalogGoodsBean catalogGoodsBean = catalogGoodsLists.get(position);

            if(catalogGoodsBean.getGoodsAdded()<catalogGoodsBean.getCount() || catalogGoodsBean.getCount() ==-1){
                catalogGoodsBean.setGoodsAdded(catalogGoodsBean.getGoodsAdded()+1);
                if (GoodsEnsurePager.onReceivedQuantityChange != null) {
                    GoodsEnsurePager.onReceivedQuantityChange.onReceivedAdd(catalogGoodsLists.get(position).getPrice(),catalogGoodsLists.get(position).getIsPackCharge(),catalogGoodsLists.get(position).getPackMoney());
                }
                notifyDataSetChanged();
            }else{
                //提示用户库存不足
                HintViewUtils.showMyToast("",R.mipmap.goods_sold_out);
            }
        }
//        private void upDateItemAdd(int position) {
//            if (GoodsEnsurePager.onReceivedQuantityChange != null) {
//                GoodsEnsurePager.onReceivedQuantityChange.onReceivedAdd(position,catalogGoodsLists.get(position).getPrice(),catalogGoodsLists.get(position).getIsPackCharge(),catalogGoodsLists.get(position).getPackMoney());
//            }
//        }

        private void upDateItemSub(int position) {
            int count = catalogGoodsLists.get(position).getGoodsAdded();
            if(count>1){
                if (GoodsEnsurePager.onReceivedQuantityChange != null) {
                    GoodsEnsurePager.onReceivedQuantityChange.onReceivedSub(catalogGoodsLists.get(position).getPrice(),catalogGoodsLists.get(position).getIsPackCharge(),catalogGoodsLists.get(position).getPackMoney());
                }
                count = count -1;
                catalogGoodsLists.get(position).setGoodsAdded(count);
                if (count <= 0) {
                    catalogGoodsLists.remove(position);
                } else {

                }
                notifyDataSetChanged();
            }
        }
//        private void upDateItemSub(int position) {
//            if (GoodsEnsurePager.onReceivedQuantityChange != null) {
//                GoodsEnsurePager.onReceivedQuantityChange.onReceivedSub(position,catalogGoodsLists.get(position).getPrice(),catalogGoodsLists.get(position).getIsPackCharge(),catalogGoodsLists.get(position).getPackMoney());
//            }
//        }
    }

    /**
     *点击X号
     */
    private static final String TAG = "GoodsEnsureAdapter";
    public void clearPosition(int position) {
        Log.e(TAG, "clearPosition:----------------------------------" );
        CatalogGoodsBean catalogGoodsBean = catalogGoodsLists.get(position);
        int itemCount = catalogGoodsBean.getGoodsAdded();
        for(int i=0;i<itemCount;i++){
            if (GoodsEnsurePager.onReceivedQuantityChange != null) {
                GoodsEnsurePager.onReceivedQuantityChange.onReceivedSub(catalogGoodsBean.getPrice(),catalogGoodsBean.getIsPackCharge(),catalogGoodsLists.get(position).getPackMoney());
            }
        }
        catalogGoodsLists.remove(position);
        notifyDataSetChanged();
    }

}
